﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.BcrReadInfos;
using Quartz;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HamcoWcs.Workers
{
    [DisallowConcurrentExecution]
    public class ReadRateWorker : WorkerBase
    {
        public static List<BcrReadInfo> ReadInfos = new List<BcrReadInfo>();

        public static object Lock = new object();

        public static bool SrmBcrReadFlag { get; set; } = false;
        public static bool CVBcrReadFlag { get; set; } = false;

        public ReadRateWorker(WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger) : base(wcsDbContext, mapper, logger)
        {
        }

        public override Task Execute(IJobExecutionContext context)
        {
            var s = context.MergedJobDataMap.GetString("eqptSymbol");
            //Console.WriteLine($"ReadRateWorker:{s}");

            if (s == "flag")
            {
                SrmBcrReadFlag = true;
                CVBcrReadFlag = true;
            }

            if (s == "queue")
            {
                if (ReadInfos.Count > 0)
                {
                    lock (Lock)
                    {
                        _wcsDbContext.Set<BcrReadInfo>().AddRange(ReadInfos);
                        _wcsDbContext.SaveChanges();
                        ReadInfos.Clear();
                    }
                }

            }


            return Task.CompletedTask;
        }
    }
}
